package com.block.channelext.service.channel;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import cn.hutool.http.HttpUtil;
import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.block.channelext.model.result.ReportResult;
import com.block.channelext.service.IChannelHandleService;
import com.block.common.service.LogFactory;
import com.block.db.entity.ChannelInfo;
import org.slf4j.Logger;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.Map;

@Service
public class BaiDuFlowChannelHandleService implements IChannelHandleService {

    private String requestUrl = "https://ocpc.baidu.com/ocpcapi/api/uploadConvertData";
    private String param_log_url = "https://m.cdpurong.com/exp/chl/1ASONC?bd_vid={}";
    private String authStr = "Bwd8o9LpszwVhzq7KdwVXAs1etooK6o1@3s1o2ub26NBrpUksHpTNeI8VGVHm4myo";

    private static Logger log = LogFactory.getCommLog();

    @Override
    public ReportResult registerAction(ChannelInfo channelInfo, JSONObject extJson) {

        String bdVid = extJson.getString("bdVid");
        if (StrUtil.isEmpty(bdVid)){
            return ReportResult.newInstance();
        }

        String logidUrl = StrUtil.format(param_log_url, bdVid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logidUrl", logidUrl);
        jsonObject.put("newType", 25);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("token", authStr);
        bizMap.put("conversionTypes", jsonArray);

        log.info("【百度信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();

        log.info("【百度信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        JSONObject header = result.getJSONObject("header");
        Integer status = header.getInteger("status");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(status));
        reportResult.setMessage(JSONObject.toJSONString(header));
        if (!ObjectUtil.equal(status, Integer.valueOf(0))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }

        return reportResult;
    }

    @Override
    public ReportResult authAction(ChannelInfo channelInfo, JSONObject extJson) {

        String bdVid = extJson.getString("bdVid");
        if (StrUtil.isEmpty(bdVid)){
            return ReportResult.newInstance();
        }

        String logidUrl = StrUtil.format(param_log_url, bdVid);


        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logidUrl", logidUrl);
        jsonObject.put("newType", 3);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("token", authStr);
        bizMap.put("conversionTypes", jsonArray);

        log.info("【百度信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();
        log.info("【百度信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        JSONObject header = result.getJSONObject("header");
        Integer status = header.getInteger("status");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(status));
        reportResult.setMessage(JSONObject.toJSONString(header));
        if (!ObjectUtil.equal(status, Integer.valueOf(0))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }

    @Override
    public ReportResult proUvAction(ChannelInfo channelInfo, JSONObject extJson) {

        String bdVid = extJson.getString("bdVid");
        if (StrUtil.isEmpty(bdVid)){
            return ReportResult.newInstance();
        }

        String logidUrl = StrUtil.format(param_log_url, bdVid);

        JSONObject jsonObject = new JSONObject();
        jsonObject.put("logidUrl", logidUrl);
        jsonObject.put("newType", 10);

        JSONArray jsonArray = new JSONArray();
        jsonArray.add(jsonObject);

        Map<String, Object> bizMap = new HashMap<>();
        bizMap.put("token", authStr);
        bizMap.put("conversionTypes", jsonArray);

        log.info("【百度信息流上报】【注册】【开始请求】【reqestUrl】{}【bizMap】{}", requestUrl, JSON.toJSONString(bizMap));
        String response = HttpUtil.createPost(requestUrl)
                .body(JSON.toJSONString(bizMap))
                .execute().body();

        log.info("【百度信息流上报】【注册】【开始结束】【result】{}", response);

        JSONObject result = JSON.parseObject(response);
        JSONObject header = result.getJSONObject("header");
        Integer status = header.getInteger("status");

        ReportResult reportResult = ReportResult.newInstance(JSON.toJSONString(bizMap), response);
        reportResult.setCode(String.valueOf(status));
        reportResult.setMessage(JSONObject.toJSONString(header));
        if (!ObjectUtil.equal(status, Integer.valueOf(0))){
            // 上报不成功报异常
            reportResult.setSuccess(true);
        }
        return reportResult;
    }
}
